from django.core.management.base import BaseCommand
from movies.models import Movie


class Command(BaseCommand):
    """初始化电影数据命令"""

    help = '向数据库中添加中文模拟电影数据'

    def handle(self, *args, **options):
        """处理命令执行"""
        # 定义要插入的电影数据
        movies_data = [
            {
                "title": "肖申克的救赎",
                "genres": ["剧情", "犯罪"]
            },
            {
                "title": "霸王别姬",
                "genres": ["剧情", "爱情"]
            },
            {
                "title": "阿甘正传",
                "genres": ["剧情", "爱情"]
            },
            {
                "title": "泰坦尼克号",
                "genres": ["剧情", "爱情"]
            },
            {
                "title": "这个杀手不太冷",
                "genres": ["剧情", "动作"]
            },
            {
                "title": "美丽人生",
                "genres": ["剧情", "喜剧"]
            },
            {
                "title": "千与千寻",
                "genres": ["动画", "奇幻"]
            },
            {
                "title": "辛德勒的名单",
                "genres": ["剧情", "历史"]
            }
        ]

        # 插入电影数据
        created_count = 0
        for movie_data in movies_data:
            # 检查是否已存在相同标题的电影
            if not Movie.objects.filter(title=movie_data["title"]).exists():
                Movie.objects.create(
                    title=movie_data["title"],
                    genres=movie_data["genres"]
                )
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'成功创建电影: {movie_data["title"]}')
                )
            else:
                self.stdout.write(
                    f'电影已存在: {movie_data["title"]}'
                )

        # 输出总结信息
        self.stdout.write(
            self.style.SUCCESS(
                f'数据初始化完成，共创建了 {created_count} 条新电影记录'
            )
        )
